package com.np.controller;

import com.np.model.Comment;
import com.np.model.OrderSheet;
import com.np.model.Product;
import com.np.model.User;
import com.np.util.HibernateUtil;
import com.sun.org.apache.regexp.internal.RE;
import org.hibernate.Session;
import org.hibernate.query.Query;
import org.springframework.aop.MethodBeforeAdvice;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RequestMethod;
import sun.plugin2.os.windows.SECURITY_ATTRIBUTES;

import javax.servlet.http.HttpSession;
import java.io.UnsupportedEncodingException;
import java.text.SimpleDateFormat;
import java.util.Date;
import java.util.List;

/**
 * Created by Administrator on 2017/4/17.
 */
@Controller
public class ProductController {

    @RequestMapping("/introduction")
    public String introduction(int id, Model model) {

        System.out.print("---------------------------------------------");
        System.out.print(id);


        Session session = HibernateUtil.getSessionFactory().openSession();
        Product product = session.load(Product.class, id);
        model.addAttribute("product", product);
        return "introduction";
    }

    @RequestMapping("/firstItem")
    public String firstItem(){
        return "fristItem";
    }


    @RequestMapping("/buy")
    public String buy(int id, int number, Model model, HttpSession httpSession) {

        System.out.print("-----------------------------" + id + "       " + number);


        Session session = HibernateUtil.getSessionFactory().openSession();
        Product product = session.load(Product.class, id);

        OrderSheet order = new OrderSheet();
        order.setOrderStatus("未付款");
        order.setPid(product.getId());
        order.setNumber(number);
        User user = (User) httpSession.getAttribute("user");
        order.setUid(user.getId());

        session.beginTransaction();
        session.save(order);
        session.getTransaction().commit();

        System.out.print("--------------------------\n");

        model.addAttribute("user", user);
        model.addAttribute("order", order);
        model.addAttribute("product", product);
        return "success";

    }

    @RequestMapping("/addAdmin")
    public String addAdmin(Model model) {
        return "admin/addAdmin";
    }

    @RequestMapping("/addAdmin_save")
    public String addAdmin_save(String email, String password) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        User user = new User();
        user.setEmail(email);
        user.setPassword(password);
        user.setLevel(1);
        session.save(user);
        session.getTransaction().commit();
        return "redirect:/addAdmin";
    }


    @RequestMapping("/lookUser")
    public String lookUser(Model model) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from User ";
        Query query = session.createQuery(hql);

        List<User> userList = query.list();
        model.addAttribute("userList", userList);


        return "admin/userlist";

    }

    @RequestMapping("/deleteUser")
    public void deleteUser(int id) {

        Session s = HibernateUtil.getSessionFactory().openSession();
        User u = s.load(User.class, id);
        s.close();

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        session.delete(u);
        session.getTransaction().commit();
     //   return "redirect:/lookUser";

    }

    /**
     * 留言
     * @param model
     * @param session1
     * @return
     */
    @RequestMapping("/comment")
    public String comment(Model model,HttpSession session1) {

        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Comment ";
        Query query = session.createQuery(hql);

        List<Comment> commentsList = query.list();
        model.addAttribute("commentsList", commentsList);
        return "admin/comment";

    }

    /***
     * 向数据库添加用户留言
     * @param question
     * @param httpSession
     * @return
     */
    @RequestMapping(value = "/addcomment",method = RequestMethod.POST)
    public String addcomment(String question, HttpSession httpSession) {
        User user = (User) httpSession.getAttribute("user");
        if(user == null){
            return "login";
        }

        Session session = HibernateUtil.getSessionFactory().openSession();
        session.beginTransaction();
        Comment comment = new Comment();
        comment.setQuestion(question);
        System.out.print("************************************************"+question);


//        try {
//            byte source [] = question.getBytes("iso8859-1");//得到客户机提交的原始数据
//            question = new String (question.getBytes("Latin-1"),"UTF-8");//解决乱码
//
//        } catch (UnsupportedEncodingException e) {
//            e.printStackTrace();
//        }


        Date currentTime = new Date();
        SimpleDateFormat formatter = new SimpleDateFormat("yyyy/MM/dd/HH:mm:ss");
        comment.setTime(formatter.format(currentTime));

        comment.setAnswerUserId(user.getId());
        comment.setQuestion(question);
        session.save(comment);
        session.getTransaction().commit();

        return "redirect:/comment";

    }

    @RequestMapping("/reply")
    public String reply(Model model) {
        Session session = HibernateUtil.getSessionFactory().openSession();
        String hql = "from Comment ";
        Query query = session.createQuery(hql);

        List<Comment> commentsList = query.list();
        model.addAttribute("commentsList", commentsList);
        return "admin/reply";
    }


    @RequestMapping("/addreply")
    public String addreply(int id,String answer) {

        System.out.print("---------------------------------------"+"\br");
        System.out.print(id+answer);

        Session session = HibernateUtil.getSessionFactory().openSession();
        Comment com = session.load(Comment.class,id);
        com.setAnswer(answer);
        session.beginTransaction();
        session.update(com);
        session.getTransaction().commit();

        return "redirect:/reply";
    }
}





